<?php
namespace plugin\ky_shouyin;

use support\Db;

class Install
{
    public function install()
    {
        // 创建静态资源目录
        $this->createStaticDirs();
        
        // 复制静态资源
        $this->copyStaticFiles();
        
        // 创建会员等级表
        $sql = "CREATE TABLE IF NOT EXISTS `member_level` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `name` varchar(50) NOT NULL COMMENT '等级名称',
            `discount` decimal(10,2) NOT NULL DEFAULT '100' COMMENT '折扣率(%)',
            `points_rate` decimal(10,2) NOT NULL DEFAULT '1' COMMENT '积分比例',
            `upgrade_points` int(11) NOT NULL DEFAULT '0' COMMENT '升级所需积分',
            `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态：0禁用 1启用',
            `remark` varchar(255) DEFAULT NULL COMMENT '备注',
            `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
            `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
            PRIMARY KEY (`id`),
            UNIQUE KEY `uk_name` (`name`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员等级表';";
        
        Db::connection()->getPdo()->exec($sql);
        
        // 添加默认会员等级
        $now = date('Y-m-d H:i:s');
        Db::table('ky_member_level')->insert([
            [
                'name' => '普通会员',
                'discount' => 100,
                'points_rate' => 1,
                'upgrade_points' => 0,
                'status' => 1,
                'created_at' => $now,
                'updated_at' => $now
            ],
            [
                'name' => '银卡会员',
                'discount' => 95,
                'points_rate' => 1.2,
                'upgrade_points' => 1000,
                'status' => 1,
                'created_at' => $now,
                'updated_at' => $now
            ],
            [
                'name' => '金卡会员',
                'discount' => 90,
                'points_rate' => 1.5,
                'upgrade_points' => 5000,
                'status' => 1,
                'created_at' => $now,
                'updated_at' => $now
            ]
        ]);
        
        // 创建会员卡表
        $sql = "CREATE TABLE IF NOT EXISTS `member_card` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `card_no` varchar(50) NOT NULL COMMENT '卡号',
            `member_id` int(11) DEFAULT NULL COMMENT '会员ID',
            `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态：0未使用 1已使用 2已作废',
            `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
            `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
            PRIMARY KEY (`id`),
            UNIQUE KEY `uk_card_no` (`card_no`),
            KEY `idx_member_id` (`member_id`),
            KEY `idx_status` (`status`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员卡表';";
        
        Db::connection()->getPdo()->exec($sql);
        
        // 创建会员表
        $sql = "CREATE TABLE IF NOT EXISTS `member` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `name` varchar(50) NOT NULL COMMENT '会员姓名',
            `phone` varchar(20) NOT NULL COMMENT '手机号',
            `level` int(11) NOT NULL DEFAULT '1' COMMENT '会员等级',
            `balance` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '余额',
            `points` int(11) NOT NULL DEFAULT '0' COMMENT '积分',
            `store_id` int(11) NOT NULL COMMENT '所属门店',
            `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态：0禁用 1启用',
            `birthday` date DEFAULT NULL COMMENT '会员生日',
            `address` varchar(255) DEFAULT NULL COMMENT '地址',
            `wechat` varchar(50) DEFAULT NULL COMMENT '微信绑定',
            `points_account` decimal(10,2) DEFAULT '0.00' COMMENT '积分账户',
            `card_times` int(11) DEFAULT '0' COMMENT '卡次余额',
            `total_points` int(11) DEFAULT '0' COMMENT '剩余积分',
            `used_points` int(11) DEFAULT '0' COMMENT '累计积分',
            `remark` varchar(255) DEFAULT NULL COMMENT '备注',
            `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
            `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
            PRIMARY KEY (`id`),
            UNIQUE KEY `uk_phone` (`phone`),
            KEY `idx_store_id` (`store_id`),
            KEY `idx_level` (`level`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员表';";
        
        Db::connection()->getPdo()->exec($sql);
        
        // 创建订单表
        $sql = "CREATE TABLE IF NOT EXISTS `order` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `order_no` varchar(50) NOT NULL COMMENT '订单号',
            `member_id` int(11) DEFAULT NULL COMMENT '会员ID',
            `store_id` int(11) NOT NULL COMMENT '门店ID',
            `staff_id` int(11) NOT NULL COMMENT '员工ID',
            `amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '订单金额',
            `points_used` int(11) NOT NULL DEFAULT '0' COMMENT '使用积分',
            `points_earned` int(11) NOT NULL DEFAULT '0' COMMENT '获得积分',
            `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态：0未完成 1已完成 2已取消',
            `remark` varchar(255) DEFAULT NULL COMMENT '备注',
            `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
            `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
            PRIMARY KEY (`id`),
            UNIQUE KEY `uk_order_no` (`order_no`),
            KEY `idx_member_id` (`member_id`),
            KEY `idx_store_id` (`store_id`),
            KEY `idx_staff_id` (`staff_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单表';";
        
        Db::connection()->getPdo()->exec($sql);
        
        // 创建订单商品表
        $sql = "CREATE TABLE IF NOT EXISTS `order_goods` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `order_id` int(11) NOT NULL COMMENT '订单ID',
            `goods_id` int(11) NOT NULL COMMENT '商品ID',
            `goods_name` varchar(100) NOT NULL COMMENT '商品名称',
            `goods_price` decimal(10,2) NOT NULL COMMENT '商品单价',
            `quantity` int(11) NOT NULL COMMENT '购买数量',
            `total_amount` decimal(10,2) NOT NULL COMMENT '小计金额',
            `discount_amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '优惠金额',
            `final_amount` decimal(10,2) NOT NULL COMMENT '实付金额',
            `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
            PRIMARY KEY (`id`),
            KEY `idx_order_id` (`order_id`),
            KEY `idx_goods_id` (`goods_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单商品表';";
        
        Db::connection()->getPdo()->exec($sql);
        
        // 创建订单支付表
        $sql = "CREATE TABLE IF NOT EXISTS `order_payment` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `order_id` int(11) NOT NULL COMMENT '订单ID',
            `payment_method` varchar(20) NOT NULL COMMENT '支付方式',
            `amount` decimal(10,2) NOT NULL COMMENT '支付金额',
            `transaction_no` varchar(100) DEFAULT NULL COMMENT '交易单号',
            `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态：0未支付 1已支付 2已退款',
            `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
            `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
            PRIMARY KEY (`id`),
            KEY `idx_order_id` (`order_id`),
            KEY `idx_transaction_no` (`transaction_no`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单支付表';";
        
        Db::connection()->getPdo()->exec($sql);
        
        // 创建积分记录表
        $sql = "CREATE TABLE IF NOT EXISTS `points_log` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `member_id` int(11) NOT NULL COMMENT '会员ID',
            `points` int(11) NOT NULL COMMENT '积分变动',
            `type` tinyint(1) NOT NULL COMMENT '类型：1消费 2签到 3兑换 4调整',
            `order_id` int(11) DEFAULT NULL COMMENT '关联订单ID',
            `description` varchar(255) DEFAULT NULL COMMENT '说明',
            `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
            PRIMARY KEY (`id`),
            KEY `idx_member_id` (`member_id`),
            KEY `idx_order_id` (`order_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='积分记录表';";
        
        Db::connection()->getPdo()->exec($sql);
        
        // 创建充值记录表
        $sql = "CREATE TABLE IF NOT EXISTS `member_recharge` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `member_id` int(11) NOT NULL COMMENT '会员ID',
            `amount` decimal(10,2) NOT NULL COMMENT '充值金额',
            `give_amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '赠送金额',
            `payment_method` varchar(20) NOT NULL COMMENT '支付方式',
            `store_id` int(11) NOT NULL COMMENT '门店ID',
            `staff_id` int(11) NOT NULL COMMENT '操作员ID',
            `remark` varchar(255) DEFAULT NULL COMMENT '备注',
            `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
            PRIMARY KEY (`id`),
            KEY `idx_member_id` (`member_id`),
            KEY `idx_store_id` (`store_id`),
            KEY `idx_staff_id` (`staff_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='充值记录表';";
        
        Db::connection()->getPdo()->exec($sql);
    }

    public function uninstall()
    {
        // 卸载时的处理
    }

    protected function createStaticDirs()
    {
        $dirs = [
            'public/static/css',
            'public/static/js/lib'
        ];

        foreach ($dirs as $dir) {
            $path = __DIR__ . '/' . $dir;
            if (!is_dir($path)) {
                mkdir($path, 0755, true);
            }
        }
    }

    protected function copyStaticFiles()
    {
        $public_path = public_path();
        $plugin_path = __DIR__;
        $dest_path = $public_path . '/app/ky_shouyin/static';

        // 确保目标目录存在
        if (!is_dir($dest_path)) {
            mkdir($dest_path, 0755, true);
        }

        // 复制整个 static 目录
        $this->copyDir($plugin_path . '/public/static', $dest_path);
    }

    protected function copyDir($src, $dst) 
    {
        if (!is_dir($src)) {
            return;
        }
        
        $dir = opendir($src);
        if (!is_dir($dst)) {
            mkdir($dst, 0755, true);
        }
        
        while(($file = readdir($dir)) !== false) {
            if (($file != '.') && ($file != '..')) {
                if (is_dir($src . '/' . $file)) {
                    $this->copyDir($src . '/' . $file, $dst . '/' . $file);
                }
                else {
                    copy($src . '/' . $file, $dst . '/' . $file);
                }
            }
        }
        closedir($dir);
    }
} 